package com.wms.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wms.tool.Contant;

public class BuildHttpClient2false {
	private HttpClientBuilder hcb = null;
	private static class BuildHttpClient{
		public static BuildHttpClient2false bc=new BuildHttpClient2false();
	} 
	public final CloseableHttpClient build(){
		return BuildHttpClient.bc.build();
	}
	private CloseableHttpClient buildHttpClient() {
        return hcb.build();
    }
	
	private static Logger logger = LoggerFactory.getLogger(BuildHttpClient2false.class);
	private static CloseableHttpClient HttpClient = null;
	private static HttpPost post = null;
	private static StringEntity requstEntity = null;
	private static HttpResponse response = null;
	
	public static HttpResponse doPost(String path, String bodyParams) throws ClientProtocolException, IOException{
		try {
			// 创建客户端
			HttpClient = HttpClients.createDefault();
			// 请求行
			post = new HttpPost(Contant.getProperties().get("pd_URL") + path.toString());
			// 请求头
			post.addHeader("token", Contant.getProperties().getProperty("token"));				
			// 请求实体
			requstEntity = new StringEntity(bodyParams);
			requstEntity.setContentEncoding("UTF-8");
			requstEntity.setContentType("application/json");// 发送json数据需要设置contentType
			post.setEntity(requstEntity);
			logger.info("这里是请求行 {}",post);
			logger.info("这里是请求实体 {}",bodyParams);
			// 响应
			response = HttpClient.execute(post);
			logger.info("这里是响应 {}",response);
			return response;
	}catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return null;
	}
}
